<template>
    <view>
        <view v-if="data_list.length > 0" class="cart-page">
            <!-- 数据列表 -->
            <view :class="'padding-horizontal-main padding-top-main '+(source_type != 'cart' ? 'bottom-line-exclude' : '')">
                <uni-swipe-action>
                    <view v-for="(item, index) in data_list" :key="index" class="oh border-radius-main bg-white spacing-mb">

                        <uni-swipe-action-item :right-options="swipe_options" @click="swipe_opt_event" @change="swipe_change($event, index)">
                          <view  v-if="item.isNew==1" style="background-color:rgba(230, 230, 230, 0.7);" :class="'cart-goods-item padding-main pr ' + (common_site_type == 1 ? 'cart-exhibition-mode-data' : '')">
                            <!-- 选择 -->
                            <view v-if="common_site_type != 1" @tap="selected_event" data-type="node" :data-index="index" class="fl cart-selected">
                              <image class="icon" :src="common_static_url+'select' + ((item.selected || false) ? '-active' : '') + '-icon.png'" mode="widthFix"></image>
                            </view>

                            <view class="items oh padding-left-sm">
                              <view  class="fl">
                                <navigator :url="item.goods_url" hover-class="none">
                                  <!-- 图片 -->
                                  <!--                                            <image style="border: 1px solid bisque;" :class="'cart-goods-image fl radius '+((item.is_error || 0) == 1 ? 'opacity' : '')" :src="item.images" mode="aspectFill"></image>
                                                                              -->
                                  <view v-if="item.images === null">
                                    <image :src="'https://zkerp.oss-cn-shenzhen.aliyuncs.com/public/'+item.title+'.png'"  style="border: 1px solid bisque;" mode="aspectFill"  :class="'cart-goods-image fl radius '+((item.is_error || 0) == 1 ? 'opacity' : '')"></image>
                                  </view>
                                  <view v-else>
                                    <image :src="item.images" style="border: 1px solid bisque;" mode="aspectFill"  :class="'cart-goods-image fl radius '+((item.is_error || 0) == 1 ? 'opacity' : '')"></image>
                                  </view>
                                </navigator>
                              </view>

                              <!-- 基础 -->
                              <view class="cart-goods-base fr">
                                <!-- 标题、规格 -->
                                <navigator :url="item.goods_url" hover-class="none">
                                  <view :class="'cart-goods-title margin-bottom-sm '+((item.is_error || 0) == 1 ? 'cr-grey' : '')">{{item.title}}</view>
                                </navigator>
                                <view v-if="item.spec != null" class="margin-bottom-sm">
                                  <text>{{item.spec}}</text>
                                </view>

                                <!-- 底部内容 -->
                                <view class="goods-bottom pr margin-top-sm">
                                  <!-- 价格 -->
                                  <text class="sales-price" style="padding-right: 4px;font-size: 12px;">{{currency_symbol}}{{item.price}}/{{item.unitName}}</text>
                                  <text> 合:<text class="sales-price" style="font-size: 12px;">{{currency_symbol}}{{towNumber(item.price*item.stock)}}</text></text>
                                  <!-- 数量 -->
                                  <view v-if="(item.is_error || 0) != 1 && common_site_type != 1" class="cart-number-content pa tc oh round br">
                                    <view @tap="goods_buy_number_event" class="number-submit tc cr-gray fl va-m" :data-index="index" data-type="0">-</view>
                                    <input  @click="openKeyBoard" style="width: 40px;background-color: beige;" disabled="true" @blur="goods_buy_number_blur" class="tc cr-gray fl va-m bg-white radius-0" type="number" :value="item.stock" :data-index="index">
                                    <view @tap="goods_buy_number_event" class="number-submit tc cr-gray fl va-m" :data-index="index" data-type="1">+</view>
                                  </view>
                                </view>
                              </view>
                            </view>
                            <view   class="fl" v-for="(item1, index1) in JSON.parse(item.materialList)" :key="index1" >
                              <button  class="mini-btn" type="default" size="mini" plain="true" @tap="change_material(index,item1.id,item1.name,item1.unitName,item1.model)" >{{item1.name}}({{item1.unitName}}){{item1.model}}</button>
                            </view>
                          </view>
                          <!--    复制 多一份  新增的物料显示颜色不同                         -->
                            <view v-else :class="'cart-goods-item padding-main pr ' + (common_site_type == 1 ? 'cart-exhibition-mode-data' : '')">
                                <!-- 选择 -->
                                <view v-if="common_site_type != 1" @tap="selected_event" data-type="node" :data-index="index" class="fl cart-selected">
                                    <image class="icon" :src="common_static_url+'select' + ((item.selected || false) ? '-active' : '') + '-icon.png'" mode="widthFix"></image>
                                </view>

                                <view class="items oh padding-left-sm">
                                    <view  class="fl">
                                        <navigator :url="item.goods_url" hover-class="none">
                                            <!-- 图片 -->
<!--                                            <image style="border: 1px solid bisque;" :class="'cart-goods-image fl radius '+((item.is_error || 0) == 1 ? 'opacity' : '')" :src="item.images" mode="aspectFill"></image>
                                            -->
											<view v-if="item.images === null">
												<image :src="'https://zkerp.oss-cn-shenzhen.aliyuncs.com/public/'+item.title+'.png'"  style="border: 1px solid bisque;" mode="aspectFill"  :class="'cart-goods-image fl radius '+((item.is_error || 0) == 1 ? 'opacity' : '')"></image>
											</view>
											<view v-else>
												<image :src="item.images" style="border: 1px solid bisque;" mode="aspectFill"  :class="'cart-goods-image fl radius '+((item.is_error || 0) == 1 ? 'opacity' : '')"></image>
											</view>

											<!-- 错误 -->
                                            <view v-if="(item.is_error || 0) == 1" class="error-msg pa tc text-size-xs">
                                                <text class="cr-red tc bg-white round">{{item.error_msg}}</text>
                                            </view>
                                        </navigator>
                                    </view>

                                    <!-- 基础 -->
                                    <view class="cart-goods-base fr">
                                        <!-- 标题、规格 -->
                                        <navigator :url="item.goods_url" hover-class="none">
                                            <view :class="'cart-goods-title margin-bottom-sm '+((item.is_error || 0) == 1 ? 'cr-grey' : '')">{{item.title}}</view>
                                        </navigator>
                                        <view v-if="item.spec != null" class="margin-bottom-sm">
											<text>{{item.spec}}</text>
                                        </view>
										<view>
											<text class="sales-price" style="padding-right: 4px;">{{currency_symbol}}{{item.price}}/{{item.unitName}}</text>
										</view>

                                        <!-- 底部内容 -->
                                        <view class="goods-bottom pr margin-top-sm">
                                            <!-- 价格 -->

											<text> 合共:<text class="sales-price">{{currency_symbol}}{{(item.price*item.stock).toFixed(2)}}</text></text>
                                            <!-- 数量 -->
                                            <view v-if="(item.is_error || 0) != 1 && common_site_type != 1" class="cart-number-content pa tc oh round br">
                                                <view @tap="goods_buy_number_event" class="number-submit tc cr-gray fl va-m" :data-index="index" data-type="0">-</view>
                                                <input  @click="openKeyBoard" style="width: 40px;background-color: beige;" disabled="true" @blur="goods_buy_number_blur" class="tc cr-gray fl va-m bg-white radius-0" type="number" :value="item.stock" :data-index="index">
                                                <view @tap="goods_buy_number_event" class="number-submit tc cr-gray fl va-m" :data-index="index" data-type="1">+</view>
                                            </view>
                                        </view>
                                    </view>
                                </view>
<!--                              <view   class="fl" v-for="(item1, index1) in materialList" :key="index1" >-->
<!--                                <button  class="mini-btn" type="default" size="mini" plain="true" @tap="change_material(index,item1.id,item1.name,item1.unitName)"  >{{item.id}}{{item1.name}}({{item1.unitName}}){{item1.id}}</button>-->
<!--                              </view>-->
                            </view>
                        </uni-swipe-action-item>
                    </view>
                </uni-swipe-action>
            </view>

            <!-- 操作导航 -->
            <!-- 展示型 -->
            <view v-if="common_site_type == 1" :class="'cart-buy-nav oh wh-auto '+(source_type != 'cart' ? 'bottom-line-exclude' : '')">
                <view class="cart-exhibition-mode padding-horizontal-main">
                    <button class="bg-main cr-white round wh-auto text-size-sm" type="default" @tap="exhibition_submit_event" hover-class="none">
                        <view class="dis-inline-block va-m margin-right-xl">
                            <uni-icons type="phone" size="14" color="#fff" />
                        </view>
                        <text class="va-m">{{common_is_exhibition_mode_btn_text}}</text>
                    </button>
                </view>
            </view>
            <!-- 销售,自提,虚拟销售 -->
            <view v-else :class="'cart-buy-nav oh wh-auto br-t bg-white '+(source_type != 'cart' ? 'bottom-line-exclude' : '')">
                <view class="cart-nav-base fl single-text padding-left">
                    <view @tap="selected_event" data-type="all" class="fl cart-selected">
                        <image class="icon va-m" :src="common_static_url+'select' + (is_selected_all ? '-active' : '') + '-icon.png'" mode="widthFix"></image>
                        <text v-if="!already_selected_status" class="va-m cr-base">全选</text>
                    </view>
                    <view v-if="already_selected_status" @tap="cart_all_remove_event" class="cart-nav-remove-submit pa bg-white cr-red br-red round cp">删除</view>
                    <view class="fr price">

                        <view class="sales-price single-text fr">{{currency_symbol}}<span v-if="total_price">{{total_price}}</span></view>
                      <view class="fr">共：</view>
                      <view class="fr art-nav-submit padding-top padding-bottom ">
                        <button class="bg-green cr-white round "  type="default"  @longpress="longpressBtn" @touchend="touchendBtn"  hover-class="none"> <text>{{longPress == '1' ? '按住说话' : '说话中...'}}</text></button>
                      </view>
                    </view>


                 </view>
                <view class="fr cart-nav-submit padding-top padding-bottom padding-horizontal-main">
                  <button class="bg-main cr-white round text-size-lg" type="default" @tap="buy_submit_event" :disabled="!already_valid_selected_status" hover-class="none">结算</button>
                </view>
            </view>



        </view>

        <!-- 空购物车 -->
        <view v-if="data_list.length == 0 && data_list_loding_status == 0" class="cart-no-data-box tc">
            <image :src="common_static_url+'cart-empty.png'" mode="widthFix" class="margin-bottom-lg"></image>
            <view class="cr-grey text-size-sm">{{data_list_loding_msg || '购物车空空如也'}}</view>
            <navigator class="dis-inline-block" :url="home_page_url" open-type="switchTab" hover-class="none">
                <button class="bg-main br-main cr-white text-size round margin-top-xxl" type="default" size="mini" hover-class="none">去逛逛</button>
            </navigator>

			<view style="position: fixed;bottom: 0;width: 100%;margin: 2px">



			  <button class="bg-green cr-white round text-size" style="width: 98%;" type="default"  @longpress="longpressBtn" @touchend="touchendBtn"  hover-class="none"> <text>{{longPress == '1' ? '按住说话' : '说话中...'}}</text></button>

			</view>
        </view>

		<!-- 语音音阶动画 -->
		<view class="prompt-layer prompt-layer-1" v-if="longPress === '2'">
		   <view class="prompt-loader">
		     <view class="em" v-for="(item,index) in 15" :key="index"></view>
		   </view>
		   <text class="span">松手结束录音</text>
		 </view>

        <!-- 提示信息 -->
        <block v-if="data_list.length == 0 && data_list_loding_status != 0">
            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
        </block>

        <!-- 用户基础 -->
        <component-user-base ref="user_base"></component-user-base>

		<!-- 数字键盘 -->
		<block v-if="keyboard_status">
			                    <view class="cart-mask wh-auto ht-auto pf" @tap="keyboard_event"></view>
			                    <view class="cart-content bg-white border-radius-main pa oh">
			                        <block>
			                            <view class="oh br-b padding-vertical-main padding-horizontal-main">
											<view sclass="input-content">&nbsp;{{ inputValue.join("") }}</view>
											<view class="confirm-btn" @click="confirm">确定</view>
			                            </view>
			                            <scroll-view :scroll-y="true" class="cart-list goods-list">
			                                <view class="item padding-main oh spacing-mb">
			                                    <view>
													  <view class="modal"  :animation="animationData1" @touchmove.stop.prevent>
														<view class=" content-wrap" :animation="animationData2">
														  <view class="key-box">
															<view
															  :class="{
																'handel-btn': ['.', '删除', ''].includes(item),
																'active-btn': activeKey === item,
																'active-handel-btn':
																  activeKey === item && ['.', '删除'].includes(item),
															  }"
															  v-for="(item, index) in keyboardList"
															  :key="index"
															  @click="setValue(item)"
															  >{{ item }}</view
															>
														  </view>
														</view>

													  </view>
												</view>
			                                </view>
			                            </scroll-view>
			                        </block>
			                    </view>
		</block>


    </view>
</template>
<script>
    const app = getApp();
    let init // 录制时长计时器
    let timer; // 播放 录制倒计时
    const recorder = wx.getRecorderManager();
    import base64 from '../../common/js/lib/base64.js';

    import componentNoData from "../../components/no-data/no-data";
    import componentUserBase from "../../components/user-base/user-base";


	var common_static_url = app.globalData.get_static_img_url();// var common_static_url = app.globalData.get_static_url('common');
    export default {
        data() {
            return {
              //语音功能引入
              flag:'',
              isScanning:false,//是否语音说话中
              longPress: '1', // 1显示 按住说话 2显示 说话中
              count: null, // 录制倒计时
              yyin:true,
              //end
				animationData1: {},
				animationData2: {},
				user: null,
                common_static_url: common_static_url,
                data_list_loding_status: 1,
                data_list_loding_msg: '',
                data_bottom_line_status: false,
                data_list: [],
                total_price: '0.00',
                is_selected_all: false,
                already_selected_status: false,
                already_valid_selected_status: false,
                source_type: null,
                // 基础配置
                currency_symbol: app.globalData.data.currency_symbol,
                common_site_type: 0,
                common_is_exhibition_mode_btn_text: null,
                common_app_customer_service_tel: null,
                swipe_item_index: null,
                swipe_options:[
                    {
                        text: '收藏',
                        style: {
                            backgroundColor: '#1AAD19'
                        }
                    },{
                        text: '删除',
                        style: {
                            backgroundColor: '#E64340'
                        }
                    }
                ],
                // 首页地址
                home_page_url: app.globalData.data.tabbar_pages[0],
				inputValue: [],
				ywid:"",
				keyboard_status:false,
            };
        },

        components: {
            componentNoData,
            componentUserBase
        },
        props: {},

        created: function() {
			var user = app.globalData.get_user_info(this, "init");
			if(user != null){
				this.user =user;
			}

            // 数据加载
            this.init();

            // 初始化配置
            this.init_config();
        },

        // 下拉刷新
        onPullDownRefresh() {
            this.init();
        },
		computed: {
			keyboardList() {
			  return this.decimals
				? ["1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "删除"]
				: ["1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "删除"];
			},
		},
        methods: {
			towNumber(val) {
				return Number(val).toFixed(2)
			},
          // 长按松开录音事件
          touchendBtn(){
            this.longPress = '1';
            this.stop();
          },
          longpressBtn(){
            this.longPress = '2';
            this.countdown(60); // 倒计时
            this.isScanning =true;
            this.start();
          },
          // 倒计时
          countdown(val){
            let _then = this;
            _then.count = Number(val);
            timer = setInterval(function() {
              if(_then.count > 0){
                _then.count--
              } else {
                _then.longPress = '1';
                clearInterval(timer);
              }
            }, 1000);
          },
          start() { //语音触摸
            this.yyin = false;
            const options = {
              sampleRate: 16000,
              numberOfChannels: 1,
              format: "PCM",
              lang: this.lang
            };
            recorder.start(options);
            recorder.onStart(() => {
              // console.log("Recording!~~~");
            });
            recorder.onError(err => {
              console.log(err);
            });
          },
          stop() { //语音结束触摸
             this.yyin = true;
            recorder.stop();
            recorder.onStop(res => {
              //
              this.audio_path = res.tempFilePath;

              const fs = wx.getFileSystemManager();
              fs.readFile({
                filePath: this.audio_path,
                encoding: 'base64',
                success: (res) => {
                  this.audio_data = res.data;
                  //this.audio_data = this.arrayBufferToBase64(res.data);
                  this.soundRecognize();
                }
              });
            });
          },
          change_material(index,mid,name ,unitName,model) {

            console.log("ddddddddddddddddddddddfffffgid",index,mid,name,unitName);
            let gid=this.data_list[index].id;
            //语音识别
            var user = app.globalData.get_user_info(this, "init");
            if(user != null){
              this.user =user;
            }
            // console.log("---------",this.audio_path)
            // console.log("---111----",this.audio_data)

            uni.request({
              url: app.globalData.get_request_url("changeMaterial"),
              method: 'POST',
              data: {
                "goodsId" : mid,
                "id" : gid,
              },
              header:{
                'Tenant-Id':this.user.role.tenantId,
                'X-Access-Token':this.user.token,
              },
              dataType: 'json',
              success: res => {
                uni.hideLoading();
                console.log("res11111111111111",res);
                console.log("11111111111111111",this.data_list)
                //做循环跟新分拣数据
                for (let i = 0; i < this.data_list.length; i++) {
                  console.log("i list id gid ",i,this.data_list[i].id,gid);
                  if (this.data_list[i].id ===gid) {

                    this.data_list[i].title =name;
                    this.data_list[i].unitName=unitName;
                    this.data_list[i].spec=model;
                  }
                }

                //更改 数组 的物料内容

                // if (res.data.code == 200) {
                //   let data = res.data.data;
                //   console.log("materialLis11222t",data);
                //
                //
                // }
              },
              fail: () => {
                uni.hideLoading();
                app.globalData.showToast('服务器请求出错');
              }
            });

          },
          soundRecognize() {
            console.log("ddddddddddddddddddddddfffff")
            //语音识别
            var user = app.globalData.get_user_info(this, "init");
            if(user != null){
              this.user =user;
            }
            const that =this;
            // console.log("---------",this.audio_path)
            // console.log("---111----",this.audio_data)

            uni.request({
              url: app.globalData.get_request_url("soundRecoAI"),
              method: 'POST',
              data: {base64data:this.audio_data,
                "customerId" : this.user.role.id,
                "createBy" : this.user.username,

              },
              header:{
                'Tenant-Id':this.user.role.tenantId,
                'X-Access-Token':this.user.token,
              },
              dataType: 'json',
              success: res => {
                uni.hideLoading();
                console.log("res11111111111111",res);


                if (res.data.code == 200) {
                  console.log("res1111111111111155555555555555555555555555555555",res);
                  let data = res.data.data;
                  let orderMsg = res.data.orderMsg;
                  let flag = res.data.flag;
                  this.flag = flag;
                  if (res.data.isAdd == 1) {
                    //需要更新購物車數據
                    //  console.log("11111111111111111",data_list)
                    let data_list = data.data || [];
                    this.setData({
                      data_list: data_list,
                      data_list_loding_status: data_list.length == 0 ? 0 : 3,
                      data_bottom_line_status: true,
                      data_list_loding_msg: '购物车空空如也'
                    });

                  }

                    if(orderMsg.trim()!=''){
                    uni.showModal({
                      title: '系统未找到对应的物料，语音下单内容为：',
                      content: orderMsg,
                      editable: true,
                      success: function (res) {
                        if (res.confirm) {
                          console.log('用户点击确定', res.content);
                          uni.request({
                            url: app.globalData.get_request_url("textAi"),
                            method: 'POST',
                            data: {
                              base64data: res.content,
                              "customerId": that.user.role.id,
                              "createBy": that.user.username,

                            },
                            header: {
                              'Tenant-Id': that.user.role.tenantId,
                              'X-Access-Token': that.user.token,
                            },
                            dataType: 'json',
                            success: res => {
                              uni.hideLoading();
                              console.log("res1111111111111100000000000000000000000000", res);


                              if (res.data.code == 200) {
                                let data = res.data.data;
                                let orderMsg = res.data.orderMsg;
                                let flag = res.data.flag;
                                this.flag = flag;
                                if (res.data.isAdd == 1) {
                                  //需要更新購物車數據

                                  let data_list = data.data || [];
                                  console.log("1111111111111111100000000000000000000000", data_list)
                                  that.setData({
                                    data_list: data_list,
                                    data_list_loding_status: data_list.length == 0 ? 0 : 3,
                                    data_bottom_line_status: true,
                                    data_list_loding_msg: '购物车空空如也'
                                  });


                                } else {
                                  uni.showToast({
                                    title: '未找到对应物料，请重新下单',
                                    duration: 4000
                                  });


                                }
                              }
                            },
                            fail: () => {
                              uni.hideLoading();
                              app.globalData.showToast('服务器请求出错');
                            }
                          });

                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });

                  }

                }
              },
              fail: () => {
                uni.hideLoading();
                app.globalData.showToast('服务器请求出错');
              }
            });

          },
          // 语音 end

			confirm() {
				//console.log("ywid===",this.ywid);
			  let buy_number = Number(this.inputValue.join(""));

			  var index =this.ywid;

              this.goods_buy_number_func(index, buy_number);

			  this.keyboard_status =false;
			},

			setValue(data) {
			  this.activeKey = data;
			  let timer = setTimeout(() => {
			    this.activeKey = "-";
			    clearTimeout(timer);
			  }, 100);

			  if (
			    !data ||
			    (data !== "删除" &&
			      this.inputValue.includes(".") &&
			      this.inputValue.join("").split(".")[1].length === this.pointLength)
			  )
			    return false;

			  if (
			    this.inputValue.length === 1 &&
			    this.inputValue[0] === "0" &&
			    !["删除", "."].includes(data)
			  ) {
			    return (this.inputValue = [data]);
			  }

			  if (data !== "删除") {
			    if (this.inputValue.includes(".")) {
			      if (
			        this.inputValue.join("").split(".")[0].length === this.maxLength
			      ) {
			        return false;
			      }
			    } else if (this.inputValue.length === this.maxLength) {
			      return false;
			    }
			  }

			  if (data === "删除") {
			    return this.inputValue.length && this.inputValue.pop();
			  }
			  if (data === ".") {
			    if (!this.inputValue.length || this.inputValue.includes("."))
			      return false;
			  }
			  this.inputValue.push(data);
			},

			keyboard_event(e) {
			    this.setData({
			        keyboard_status: !this.keyboard_status
			    });
			},
			openKeyBoard(e){
				var goodid =e.currentTarget.dataset.index || 0;
				console.log("goodid=",goodid);
				this.ywid =goodid;
				//this.showDigitKeyboard =false;
				//
				//清空已输入的数字
				this.inputValue = [];
				this.keyboard_status =true;
			},
            // 初始化配置
            init_config(status) {
                if ((status || false) == true) {
                    this.setData({
                        currency_symbol: app.globalData.get_config('currency_symbol'),
                        common_site_type: app.globalData.get_config('config.common_site_type'),
                        common_is_exhibition_mode_btn_text: app.globalData.get_config('config.common_is_exhibition_mode_btn_text', '立即咨询'),
                        common_app_customer_service_tel: app.globalData.get_config('config.common_app_customer_service_tel')
                    });
                } else {
                    app.globalData.is_config(this, 'init_config');
                }
            },

            // 获取数据
            init(source_type = null) {
                this.setData({source_type: source_type});
				//console.log("----------init--------------");
                var user = app.globalData.get_user_info(this, "init");
                if (user != false) {
                    // 用户未绑定用户则转到登录页面
                    if (app.globalData.user_is_need_login(user)) {
                        uni.stopPullDownRefresh();
                        uni.showModal({
                            title: '温馨提示',
                            content: '绑定手机号码',
                            confirmText: '确认',
                            cancelText: '暂不',
                            success: result => {
                                if (result.confirm) {
                                    uni.navigateTo({
                                        url: "/pages/login/login?event_callback=init"
                                    });
                                } else {
                                    this.setData({
                                        data_list_loding_status: 0,
                                        data_bottom_line_status: false,
                                        data_list_loding_msg: '请绑定手机号码'
                                    });
                                }
                            }
                        });

                        // 分享菜单处理
                        app.globalData.page_share_handle();
                    } else {
                        // 获取数据
                        this.get_data();

                        // 用户头像和昵称设置提示
                        if((this.$refs.user_base || null) != null) {
                            var self = this;
                            setTimeout(function() {
                                self.$refs.user_base.init('cart');
                            }, 10);
                        }
                    }
                } else {
                    uni.stopPullDownRefresh();
                    this.setData({
                        data_list_loding_status: 0,
                        data_bottom_line_status: false,
                        data_list_loding_msg: '请先授权用户信息'
                    });

                    // 分享菜单处理
                    app.globalData.page_share_handle();
                }
            },

            // 获取数据
            get_data() {
                this.setData({
                    total_price: '0.00',
                    is_selected_all: false,
                    already_selected_status: false
                });
                uni.request({
                    url: app.globalData.get_request_url("getCartList"),
                    method: 'POST',
					data: {
						"customerId" : this.user.role.id,
						"createBy" : this.user.username,
					},
					header:{
						'Tenant-Id':this.user.role.tenantId,
						'X-Access-Token':this.user.token,
					},
                    dataType: 'json',
                    success: res => {
						if(res.statusCode == 401){
							app.globalData.remove_user_cache_event_noToast();
							return;
						}
						console.log("get_data(res)",res);
                        uni.stopPullDownRefresh();
                        if (res.data.code == 0) {
                            var data = res.data.data;
                            var data_list = data.data || [];
                            if(data_list.length > 0) {
                                // 选中状态处理
                                var not_use = uni.getStorageSync(app.globalData.data.cache_user_cart_not_use_data_key) || {};
                                for(var i in data_list) {
                                    if((not_use[data_list[i]['id']] || null) == null || not_use[data_list[i]['id']] != data_list[i]['stock']) {
                                        data_list[i]['selected'] = true;
                                    }
                                }

                                // 缓存记录用户未选中的购物车数据
                                uni.setStorageSync(app.globalData.data.cache_user_cart_not_use_data_key, not_use);
                            }

                            // 设置数据
                            this.setData({
                                data_list: data_list,
                                data_list_loding_status: data_list.length == 0 ? 0 : 3,
                                data_bottom_line_status: true,
                                data_list_loding_msg: '购物车空空如也'
                            });

                            // 选择处理
                            this.cart_selected_calculate();

                            // 导航购物车处理
                            var cart_total = data.buy_number || 0;
                            if (cart_total <= 0) {
                                app.globalData.set_tab_bar_badge(2, 0);
                            } else {
                                app.globalData.set_tab_bar_badge(2, 1, cart_total);
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 2,
                                data_bottom_line_status: false,
                                data_list_loding_msg: res.data.msg
                            });
                            if (app.globalData.is_login_check(res.data, this, 'get_data')) {
                                app.globalData.showToast(res.data.msg);
                            }
                        }

                        // 分享菜单处理、延时执行，确保基础数据已加载完成
                        setTimeout(function() {
                            app.globalData.page_share_handle();
                        }, 3000);
                    },
                    fail: () => {
                        uni.stopPullDownRefresh();
                        this.setData({
                            data_list_loding_status: 2,
                            data_bottom_line_status: false,
                            data_list_loding_msg: '服务器请求出错'
                        });
                        app.globalData.showToast('服务器请求出错');
                    }
                });
            },

            // 数量输入事件
            goods_buy_number_blur(e) {
                var index = e.currentTarget.dataset.index || 0;
                var buy_number = parseFloat(e.detail.value) || 1;
                this.goods_buy_number_func(index, buy_number);
            },

            // 数量操作事件
            goods_buy_number_event(e) {
                var index = e.currentTarget.dataset.index || 0;
                var type = parseInt(e.currentTarget.dataset.type) || 0;
                var temp_number = parseFloat(this.data_list[index]['stock']);
                var buy_number = (type == 0) ? temp_number - 1 : temp_number + 1;
                this.goods_buy_number_func(index, buy_number);
            },

            // 数量处理方法
            goods_buy_number_func(index, buy_number) {
                var temp_data_list = this.data_list;
                var buy_min_number = parseInt(temp_data_list[index]['spec_buy_min_number']) || 1;
                var buy_max_number = parseInt(temp_data_list[index]['spec_buy_max_number']) || 0;
                var inventory = parseInt(temp_data_list[index]['inventory']);
                var inventory_unit = temp_data_list[index]['inventory_unit'];
                // if (buy_number < buy_min_number) {
                //     buy_number = buy_min_number;
                //     if (buy_min_number > 1) {
                //         app.globalData.showToast('起购' + buy_min_number + inventory_unit);
                //         return false;
                //     }
                // }

                // if (buy_max_number > 0 && buy_number > buy_max_number) {
                //     buy_number = buy_max_number;
                //     app.globalData.showToast('限购' + buy_max_number + inventory_unit);
                //     return false;
                // }

                // if (buy_number > inventory) {
                //     buy_number = inventory;
                //     app.globalData.showToast('库存数量' + inventory + inventory_unit);
                //     return false;
                // }

                // if (temp_data_list[index]['stock'] == 1 && buy_number == 1) {
                //     return false;
                // }

                // 更新数据库
                uni.showLoading({
                    title: '处理中...',
                    mask: true
                });
                uni.request({
                    url: app.globalData.get_request_url("cartUpdate"),
                    method: 'POST',
                    data: {
                        "id": temp_data_list[index]['id'],
                        "goodsId": temp_data_list[index]['goods_id'],
                        "fqty": buy_number
                    },
					header:{
						'Tenant-Id':this.user.role.tenantId,
						'X-Access-Token':this.user.token,
					},
                    dataType: 'json',
                    success: res => {
                        uni.hideLoading();
						console.log("res",res);
                        if (res.data.code == 200) {
                            var not_use = uni.getStorageSync(app.globalData.data.cache_user_cart_not_use_data_key) || {};
                            var data = res.data.data;
                            temp_data_list[index]['stock'] = data.stock;
                            temp_data_list[index]['selected'] = true;
                            delete not_use[temp_data_list[index]['id']];
                            this.setData({
                                data_list: temp_data_list
                            });

                            // 缓存记录用户未选中的购物车数据
                            uni.setStorageSync(app.globalData.data.cache_user_cart_not_use_data_key, not_use);

                            // 选择处理
                            this.cart_selected_calculate();
                        } else {
                            if (app.globalData.is_login_check(res.data)) {
                                app.globalData.showToast(res.data.msg);
                            } else {
                                app.globalData.showToast('提交失败，请重试！');
                            }
                        }
                    },
                    fail: () => {
                        uni.hideLoading();
                        app.globalData.showToast('服务器请求出错');
                    }
                });
            },

            // 收藏+删除
            goods_favor_delete(id, goods_id, type) {
                uni.request({
                    url: app.globalData.get_request_url('favor', 'goods'),
                    method: 'POST',
                    data: {
                        "id": goods_id,
                        "is_mandatory_favor": 1
                    },
                    dataType: 'json',
                    success: res => {
                        if (res.data.code == 0) {
                            this.cart_delete(id, type);
                        } else {
                            if (app.globalData.is_login_check(res.data)) {
                                app.globalData.showToast(res.data.msg);
                            } else {
                                app.globalData.showToast('提交失败，请重试！');
                            }
                        }
                    },
                    fail: () => {
                        app.globalData.showToast('服务器请求出错');
                    }
                });
            },

            // 批量删除操作
            cart_all_remove_event(e) {
                uni.showModal({
                    title: '温馨提示',
                    content: '挑了这么久，真的要删除吗？',
                    confirmText: '确认',
                    cancelText: '暂不',
                    success: result => {
                        if (result.confirm) {
                            var data = [];
                            var temp_data_list = this.data_list || [];
                            for (var i in temp_data_list) {
                                if ((temp_data_list[i]['selected'] || false) == true) {
                                    data.push(temp_data_list[i]['id']);
                                }
                            }
                            if(data.length <= 0) {
                                app.globalData.showToast('请先选择数据');
                                return false;
                            }
                            this.cart_delete(data.join(','), 'delete');
                        }
                    }
                });
            },

            // 滑动事件
            swipe_change(e, v) {
                this.setData({
                   swipe_item_index:  (e == 'none') ? null : v
                });
            },

            // 移除操作事件
            swipe_opt_event(e) {
                var index = e.index || 0;
                var temp_data_list = this.data_list;
                if(this.swipe_item_index === null) {
                    app.globalData.showToast("请先滑动要操作的数据");
                    return false;
                }
                if((temp_data_list[this.swipe_item_index] || null) == null) {
                    app.globalData.showToast("数据不存在");
                    return false;
                }

                // 根据操作类型处理数据
                var id = temp_data_list[this.swipe_item_index]['id'];
                if (index == 0) {
                    var goods_id = temp_data_list[this.swipe_item_index]['goods_id'];
                    this.goods_favor_delete(id, goods_id, 'favor');
                } else {
                    this.cart_delete(id, 'delete');
                }
            },

            // 购物车删除
            cart_delete(id, type) {
                uni.request({
                    url: app.globalData.get_request_url('cartDelete'),
                    method: 'POST',
                    data: {
                        "id": id
                    },
					header:{
						'Tenant-Id':this.user.role.tenantId,
						'X-Access-Token':this.user.token,
					},
                    dataType: 'json',
                    success: res => {
                        if (res.data.code == 200) {
                            var temp_list = [];
                            var temp_id_arr = id.split(',');
                            var temp_data_list = this.data_list;
                            for(var i in temp_data_list) {
                                if(temp_id_arr.indexOf(temp_data_list[i]['id']) == -1) {
                                    temp_list.push(temp_data_list[i]);
                                }
                            }
                            this.setData({
                                data_list: temp_list,
                                data_list_loding_status: temp_list.length == 0 ? 0 : this.data_list_loding_status
                            });
                            app.globalData.showToast(type == 'delete' ? '删除成功' : '收藏成功', 'success');

                            // 选择处理
                            this.cart_selected_calculate();
                        } else {
                            if (app.globalData.is_login_check(res.data)) {
                                app.globalData.showToast(type == 'delete' ? '删除失败' : '收藏失败');
                            } else {
                                app.globalData.showToast('提交失败，请重试！');
                            }
                        }
                    },
                    fail: () => {
                        app.globalData.showToast('服务器请求出错');
                    }
                });
            },

            // 选中处理
            selected_event(e) {
                var type = e.currentTarget.dataset.type || null;
                if (type != null) {
                    var temp_data_list = this.data_list;
                    var temp_is_selected_all = this.is_selected_all;
                    var not_use = uni.getStorageSync(app.globalData.data.cache_user_cart_not_use_data_key) || {};
                    switch (type) {
                        // 批量操作
                        case 'all':
                            temp_is_selected_all = temp_is_selected_all == true ? false : true;
                            for (var i in temp_data_list) {
                                temp_data_list[i]['selected'] = temp_is_selected_all;
                                // 选中清除则则记录
                                if(temp_is_selected_all) {
                                    delete not_use[temp_data_list[i]['id']];
                                } else {
                                    not_use[temp_data_list[i]['id']] = temp_data_list[i]['stock'];
                                }
                            }
                            break;
                            // 节点操作
                        case 'node':
                            var index = e.currentTarget.dataset.index || 0;
                            temp_data_list[index]['selected'] = temp_data_list[index]['selected'] == true ? false : true;
                            // 选中清除则则记录
                            if(temp_data_list[index]['selected']) {
                                delete not_use[temp_data_list[index]['id']];
                            } else {
                                not_use[temp_data_list[index]['id']] = temp_data_list[index]['stock'];
                            }
                            break;
                    }
                    this.setData({
                        data_list: temp_data_list,
                        is_selected_all: temp_is_selected_all,
                    });

                    // 缓存记录用户未选中的购物车数据
                    uni.setStorageSync(app.globalData.data.cache_user_cart_not_use_data_key, not_use);

                    // 选择处理
                    this.cart_selected_calculate();
                }
            },

            // 选中计算
            cart_selected_calculate() {
                var cart_total = 0;
                var total_price = 0;
                var data_count = 0;
                var selected_count = 0;
                var temp_data_list = this.data_list;
                for (var i in temp_data_list) {
                    if ((temp_data_list[i]['selected'] || false) == true) {
                        selected_count++;
                        if ((temp_data_list[i]['is_error'] || 0) == 0) {
                            data_count++;
                            total_price += parseInt(temp_data_list[i]['stock']) * parseFloat(temp_data_list[i]['price']);
                        }
                    }
                    cart_total += parseInt(temp_data_list[i]['stock']);
                }

                this.setData({
                    total_price: total_price.toFixed(2),
                    already_selected_status: selected_count > 0,
                    already_valid_selected_status: data_count > 0,
                    is_selected_all: selected_count > 0 && selected_count >= temp_data_list.length
                });
                if (cart_total <= 0) {
                    app.globalData.set_tab_bar_badge(2, 0);
                } else {
                    app.globalData.set_tab_bar_badge(2, 1, cart_total);
                }
            },
          // 结算
          // change_material(gid,mid) {
          //
          //   const jsonString = '[{"name":"Alice222","age":20},{"name":"Bob2222","age":25}]';
          //   const jsonArray = JSON.parse(jsonString);
          //   console.log("jsonArray",jsonArray);
          //   console.log("gid66666666666",gid,mid);
          //
          //
          // },
            // 结算
            buy_submit_event(e) {
                var selected_count = 0;
                var ids = [];
                var temp_data_list = this.data_list || [];
                for (var i in temp_data_list) {
                    if ((temp_data_list[i]['is_error'] || 0) == 0 && (temp_data_list[i]['selected'] || false) == true) {
                        ids.push(temp_data_list[i]['id']);
                        selected_count++;
                    }
                }
                if (selected_count <= 0) {
                    app.globalData.showToast('请先选择商品');
                    return false;
                }

                // 进入订单确认页面
                var data = {
                    "buy_type": "cart",
                    "ids": ids.join(',')
                };
				console.log("ids",ids.join(','));
                uni.navigateTo({
                    url: '/pages/buy/buy?data=' + encodeURIComponent(base64.encode(JSON.stringify(data)))
                });
            },

            // 展示型事件
            exhibition_submit_event(e) {
                app.globalData.call_tel(this.common_app_customer_service_tel);
            }
        }
    };
</script>
<style>
    /**
    * 商品列表
    */
    .cart-page {
        padding-bottom: 100rpx;
    }
    .cart-goods-title {
        line-height: 44rpx;
        min-height: 43rpx;
    }
    .cart-goods-image {
        width: 125rpx;
        height: 125rpx;
    }
    .cart-goods-base {
        width: calc(100% - 140rpx);
    }
    .cart-goods-item .selected {
        margin-top: 60rpx;
    }

    /**
     * 错误提示
     */
    .cart-goods-item .error-msg {
        left: 80rpx;
        top: 78rpx;
        width: 140rpx;
        filter: blur(0.3px);
    }
    .cart-goods-item .error-msg text {
        padding: 2rpx 10rpx;
        box-shadow: 0 2px 10px rgb(181 181 181 / 95%);
    }

    /**
     * 底部
     */
    .cart-goods-item .goods-bottom {
        height: 58rpx;
    }

    /**
    * 数量操作
    */
    .cart-number-content {
        right: 0;
        top: 0;
    }
    .cart-number-content .number-submit {
        width: 60rpx;
        font-weight: bold;
    }
    .cart-number-content input {
        width: 30px;
        border-width: 0 1px;
        border-style: solid;
        border-color: #efefef;
    }
    .cart-number-content .number-submit,
    .cart-number-content input {
        padding: 0;
        height: 50rpx;
        line-height: 50rpx;
    }

    /**
    * 空购物车
    */
    .cart-no-data-box {
        padding: 30% 0 0 0;
    }
    .cart-no-data-box image {
        width: 160rpx;
    }

    /**
    * 操作导航
    */
    .cart-buy-nav {
        position: fixed;
        z-index: 2;
        left: 0;
        bottom: 0;
        /* #ifdef H5 || APP */
        bottom: var(--window-bottom);
        /* #endif */
    }
    .cart-buy-nav,
    .cart-nav-base {
        height: 100rpx;
        line-height: 100rpx;
    }
    .cart-nav-submit button {
        height: 72rpx;
        line-height: 72rpx;
    }
    .cart-nav-base {
        width: calc(75% - 20rpx);
    }
    .cart-nav-submit {
        width: calc(25% - 40rpx);
    }
    .cart-nav-submit button {
        border-radius: 0;
    }
    .cart-selected .icon {
        width: 35rpx;
        height: 35rpx !important;
        margin: 0 10rpx;
    }
    .cart-buy-nav .price {
        width: calc(100% - 140rpx);
    }
    .cart-buy-nav .sales-price {
        max-width: calc(100% - 40px);
    }
    .cart-nav-remove-submit {
        top: 38rpx;
        left: 76rpx;
        padding: 0rpx 20rpx;
        line-height: 36rpx;
    }

    /*
    * 展示型
    */
    .cart-exhibition-mode button {
        line-height: 80rpx;
    }
    .cart-exhibition-mode-data .items {
        padding-left: 0;
    }
    .cart-exhibition-mode-data .error-msg {
        left: 26rpx;
    }
	.input-content {
	        padding-left: 50upx;
			padding-top: 10upx;
	        color: #147ff2;
	        line-height: 80upx;
	        height: 30px;
			font-size: 30upx;

	      }
	      .confirm-btn {
	        padding-right: 50upx;
			padding-top: 10upx;
	        color: #147ff2;
	        line-height: 80upx;
			font-size: 30upx;
	        position: absolute;
	        top: 0;
	        right: 0;
	      }
	.key-box {
	      margin-top: 60upx;
	      display: flex;
	      flex-flow: wrap;
	      width: 100%;
	}
	.key-box view {
	        width: 221upx;
	        height: 100upx;
	        border: 1upx solid rgb(235, 237, 240);
	        box-sizing: border-box;
	        text-align: center;
	        line-height: 100upx;
	        font-size: 40upx;
	        font-weight: bold;
	        margin: 0 -1upx -1upx 0;
	      }
		  /**
		   * 购物车
		   */
		  .cart-mask {
		      left: 0;
		      top: 0;
		      background: rgb(0, 0, 0, 0.6);
		      z-index: 5;
		  }
		  .cart-content {
		      left: auto;
		      bottom: 130rpx;
		      width: calc(100% - 40rpx);
		      z-index: 6;
		      max-width: calc(800px - 40rpx);
		      margin-left: 20rpx;
		  }
		  .cart-content .cart-list {
		      max-height: 60vh;
		  }
		  .cart-content .cart-list .goods-img {
		      width: 120rpx;
		      height: 120rpx !important;
		  }
		  .cart-content .cart-list .goods-base {
		      width: calc(100% - 140rpx);
		  }
		  .cart-content .cart-list .goods-base-content {
		      min-height: 60rpx;
		  }
		  /* 提示小弹窗 */
		  .prompt-layer{
		    border-radius: 8px;
		    background: #FFD300;
		    padding: 8px 16px;
		    box-sizing: border-box;
		    position: absolute;
		    left: 50%;
		    transform: translateX(-50%);
		  }
		  .prompt-layer::after{
		    content: '';
		    display: block;
		    border: 6px solid rgba(0,0,0,0);
		    border-top-color: rgba(255, 211, 0, 1);
		    position: absolute;
		    bottom: -10px;
		    left: 50%;
		    transform: translateX(-50%);
		  }
		  .prompt-layer-1{
		    font-size: 12px;
		    width: 128px;
		    text-align: center;
		    display: flex;
		    flex-direction: column;
		    align-items: center;
		    justify-content: center;
		    top: -80px;
		  }
		  .prompt-layer-1 .p{
		    color: #000000;
		  }
		  .prompt-layer-1 .span{
		    color: rgba(0,0,0,.6);
		  }
		  .prompt-loader .em{

		  }
</style>
